package com.unitechs.trap.excel.webapi.service.impl.entity;

import com.unitechs.trap.common.pojo.trap.entity.AllData;
import com.unitechs.trap.common.pojo.trap.entity.TrapVarBind;
import com.unitechs.trap.common.pojo.trap.entity.VarData;
import com.unitechs.trap.excel.service.ITrapVarBindExcelService;
import com.unitechs.trap.excel.webapi.mapper.AllDataMapper;
import com.unitechs.trap.excel.webapi.mapper.ReadAlaOutMapper;
import com.unitechs.trap.excel.webapi.mapper.VarDataMapper;
import com.unitechs.trap.excel.webapi.utils.StreamUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@DubboService
@Slf4j
@Service
public class TrapVarBindExcelServiceImpl implements ITrapVarBindExcelService {
    @Autowired
    VarDataMapper varDataMapper;

    @Override
    public List<TrapVarBind> listTrapVarBind() {
        List<VarData> allData = varDataMapper.listVarData();
        allData=deduplicationWithJDBC(allData);//一次去重
        List<TrapVarBind> trapVarBinds = new ArrayList<>();
        allData.forEach(data -> {
            TrapVarBind trapVarBind = new TrapVarBind();
            trapVarBind.setTrapOid(data.getTrapOid());
            trapVarBind.setVarOid(data.getVarOid());
            trapVarBinds.add(trapVarBind);
        });
        return trapVarBinds;
    }

    private List<VarData> deduplication(List<VarData> varData){
        Set<String> uniqueOids = new HashSet<>();
        List<VarData> filteredList = new ArrayList<>();
        for (VarData s : varData) {
            String a =s.getVarOid()+s.getTrapOid();
            if (uniqueOids.add(a)) {
                filteredList.add(s);

            }

        }
        return filteredList;
    }
    @Autowired
    private ReadAlaOutMapper mapper;
    private List<VarData> deduplicationWithJDBC(List<VarData> varData){
        List<TrapVarBind> trapVarBinds = mapper.listTrapVarBind();
        Set<String> uniqueOids = new HashSet<>();

        for (TrapVarBind trapVarBind:trapVarBinds){
            uniqueOids.add(trapVarBind.getTrapOid()+trapVarBind.getVarOid());
        }

        List<VarData> filteredList = new ArrayList<>();
        for (VarData s : varData) {
            String a =s.getVarOid()+s.getTrapOid();
            if (uniqueOids.add(a)) {
                filteredList.add(s);
            }

        }
        return filteredList;

    }
}
